package com.cfm880.latte.core.net;

import android.content.Context;

import com.cfm880.latte.core.net.callback.IError;
import com.cfm880.latte.core.net.callback.IFailure;
import com.cfm880.latte.core.net.callback.IRequest;
import com.cfm880.latte.core.net.callback.ISuccess;
import com.cfm880.latte.core.ui.LoaderStyle;

import java.io.File;
import java.util.Map;
import java.util.WeakHashMap;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * Created by chengfangming on 2018/1/6.
 */

public class RestClientBuilder {
    private static final WeakHashMap<String, Object> mParams = RestCreator.getParams();
    private String mUrl;
    private IRequest mRequest;
    private String mDownloadDir;
    private String mExtension;
    private String mName;
    private ISuccess mSuccess;
    private IFailure mFailure;
    private IError mError;
    private RequestBody mBody;
    private Context mContext;
    private LoaderStyle mLoaderStyle;
    private File mFile;

    public RestClientBuilder() {
    }


    public final RestClientBuilder url(String url) {
        this.mUrl = url;
        return this;
    }

    public final RestClientBuilder params(Map<String, Object> params) {
        this.mParams.putAll(params);
        return this;
    }

    public final RestClientBuilder params(String key, Object value) {
        this.mParams.put(key, value);
        return this;
    }

    public final RestClientBuilder raw(String raw) {
        this.mBody = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"), raw);
        return this;
    }

    public final RestClientBuilder success(ISuccess success) {
        this.mSuccess = success;
        return this;
    }

    public final RestClientBuilder failure(IFailure failure) {
        this.mFailure = failure;
        return this;
    }

    public final RestClientBuilder error(IError error) {
        this.mError = error;
        return this;
    }


    private final RestClientBuilder onRequest(IRequest request) {
        this.mRequest = request;
        return this;
    }

    private final RestClientBuilder file(File file) {
        this.mFile = file;
        return this;
    }

    private final RestClientBuilder downloadeDir(String downloadDir){
        this.mDownloadDir = downloadDir;
        return this;
    }

    private final RestClientBuilder extension(String extension){
        this.mExtension = extension;
        return this;
    }

    private final RestClientBuilder name(String name){
        this.mName = name;
        return this;
    }

    private final RestClientBuilder file(String  file) {
        this.mFile = new File(file);
        return this;
    }


    public final RestClientBuilder loader(Context context, LoaderStyle loaderStyle){
        this.mContext = context;
        this.mLoaderStyle = loaderStyle;
        return this;
    }

    public final RestClientBuilder loader(Context context){
        this.mContext = context;
        this.mLoaderStyle = LoaderStyle.BallClipRotatePulseIndicator;
        return this;
    }

    public final RestClient build() {
        return new RestClient(mUrl,
                mParams,
                mRequest,
                mDownloadDir,
                mExtension,
                mName,
                mSuccess,
                mFailure,
                mError, mBody,
                mLoaderStyle,
                mFile,
                mContext);
    }

}
